/* eslint-disable no-unused-vars */
import { message as Msg } from 'ant-design-vue';

import Cookie from 'js-cookie'
import store from '@/store'
import {logout, logout1} from "../services/user";

// 响应拦截
const resp = {
  onFulfilled(response, options) {
    if (response.request.responseType === 'blob') {
      const disposition = response.headers['content-disposition'].replace(/\\"/g, '').split('=')
      const fileName = decodeURI(disposition[disposition.length - 1])
      const blob = new Blob([response.data], { type: response.data.type })
      const downloadElement = document.createElement('a')
      const href = window.URL.createObjectURL(blob) // 创建下载的链接
      downloadElement.href = href
      downloadElement.download = fileName // 下载后文件名
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
      window.URL.revokeObjectURL(href) // 释放blob对象
    }
    // console.log("options.route.path",options.router.history.current.path);
    // amis页面消息处理
    if(options.router.history.current.path.indexOf("/lxnsyPortalManage") > -1
        || options.router.history.current.path.indexOf("/lxnsyTechnologyManage") > -1
        || options.router.history.current.path.indexOf("/lxnsySalesManage") > -1
        || options.router.history.current.path.indexOf("/lxnsySyManage") > -1
        || options.router.history.current.path.indexOf("/lxnsyBzhManage") > -1
        || options.router.history.current.path.indexOf("/lxnsyScreenApp") > -1){
      if(response && response.status == 200 && (response.data.status===10113 || response.data.status === 210007)){
        logout1();
      }
      return response
    } else {  // vue页面消息处理
      if(response && response.status == 200){
        if(response.data.status===0){
          if(response.config.method!=="get"){
           Msg.success(response.data.msg);
          }
        }else if(response.data.status===10113){
          logout1();
        } else if (response.data.status === 210007) {
          logout1();
        }
        else{
          if(response.data.msg){
            Msg.error(response.data.msg);
          }
          // Msg.error(response.data.msg);
        }
      }
    }
    return response;
  }
}

// 401拦截
const resp401 = {
  /**
   * 响应数据之前做点什么
   * @param response 响应对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {*}
   */
  onFulfilled(response, options) {
    console.log(response)
    const {message} = options
    if(response.data.status!==0){
      Msg.error(response.data.msg)
      if(response.data.status===10113){
        logout1()
      }
      return Promise.reject({response:response})
      // return Promise.reject({response: {status: 0,msg: response.data.msg, data: {msg: response.data.msg}}})
    }
    if (response.code === 401) {
      Msg.error('无此权限')
    }
    return response
  },
  /**
   * 响应出错时执行
   * @param error 错误对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {Promise<never>}
   */
  onRejected(error, options) {
    const {message} = options
    const {response} = error
    if (response.status === 401) {
      message.error('无此权限')
    }
    return Promise.reject(error)
  }
}

const resp403 = {
  onFulfilled(response, options) {
    const {message} = options
    if(response.data.status!==0){
      message.error(response.data.msg)
      if(response.data.status===10113){
        logout1()
      }
      return Promise.reject(response)
    }
    if (response.code === 403) {
      message.error('请求被拒绝')
    }
    return response
  },
  onRejected(error, options) {
    const {message} = options
    const {response} = error
    console.log(response);
    if (response.status === 403) {
      message.error('请求被拒绝')
    }
    return Promise.reject(error)
  }
}

const reqCommon = {
  /**
   * 发送请求之前做些什么
   * @param config axios config
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {*}
   */
  onFulfilled(config, options) {
    const {message} = options
    const {url} = config
    // console.log("1===",url)
      // console.log("2===",url.indexOf('register'))
      // console.log("1===",url.indexOf('login'))

    // if (url.indexOf('login') !== -1 && url.indexOf('register') !== -1 && !Cookie.get(process.env.VUE_APP_TOKEN)) {
    //   message.warning('认证 token 已过期，请重新登录')
    // }


    if(!config.params){
      config.params =  {}
    }
    if(store.getters['account/user']){
      config.params['loginUserId'] = store.getters['account/user'].loginUserId
      config.params['tenantCode'] =  config.params.tenantCode?config.params.tenantCode:sessionStorage.getItem("tenantCode")
      config.params['subjectInfoId'] =  config.params.subjectInfoId?config.params.subjectInfoId:sessionStorage.getItem("subjectInfoId")

    }
    return config
  },
  /**
   * 请求出错时做点什么
   * @param error 错误对象
   * @param options 应用配置 包含: {router, i18n, store, message}
   * @returns {Promise<never>}
   */
  onRejected(error, options) {
    const {message} = options
    message.error(error.message)
    return Promise.reject(error)
  }
}

export default {
  request: [reqCommon], // 请求拦截
  response: [resp] // 响应拦截
}
